<?php
/**
 * 测试pdosql方法.
 * User: halloheihei
 * Date: 2018/12/12
 * Time: 21:34
 */

class PdoClassTest
{
    private $mysqlpdo;
    public function __construct()
    {
        $this->mysqlpdo = new \coreLib\db\MysqlClass\PdoSql([
            "username"    => 'liweixuan',
            "password"    => '12345678',
            "port"        => 3306,
            "host"        => 'rm-2ze1kg3ip763wer90o.mysql.rds.aliyuncs.com',
            "dbname"   	  => 'o_o'
        ]);
    }

    public function AddTest()
    {
        //插入
        $data = array(
            'sid'=>101,
            'aa'=>123456,
            'bbc'=>'aaaaaaaaaaaaaa',
        );
        try {
            $res = $this->mysqlpdo->insert('t_table',$data);
        }catch (PDOException $exception) {

        }

        return $res;
    }

    public function queryTest()
    {
        //查询
        $res = $this->mysqlpdo->field(array('sid','aa','bbc'))
            ->order(array('sid'=>'desc','aa'=>'asc'))
            ->where(array('sid'=>"101",'aa'=>array('123455','>','or')))
            ->limit(1,2)
            ->select('t_table');
        return $res;
    }

    public function transTest()
    {
        //事务
        $this->mysqlpdo->startTrans();
        $this->mysqlpdo->where(array('sid'=>102))->update('t_table',array('aa'=>666666));
        $this->mysqlpdo->where(array('sid'=>103))->update('t_table',array('bbc'=>'呵呵8888呵呵'));
        $this->mysqlpdo->where(array('sid'=>104))->delete('t_table');
        $this->mysqlpdo->commit();

        return true;
    }

    public function getLastSqlTest()
    {
        //获取最后执行的sql语句
        $sql = $this->mysqlpdo->getLastSql();
        return $sql;
    }

    public function excuteSqlTest()
    {
        //直接执行sql语句
        $sql = "show tables";
        $res = $this->mysqlpdo->doSql($sql);

        return $res;
    }
}